function Template() {
    this.table = this.InitTable();
    this.InitForm();
}

Template.prototype.InitTable = function () {
    var table;
    layui.use('table', function () {
        table = layui.table;
        table.init('parse-table-demo', {});
    });
    return table;
}

Template.prototype.InitForm = function () {
    layui.use(['form', 'element', 'laydate'], function () {
        var form = layui.form;
        var element = layui.element;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#begintime'
        });
        laydate.render({
            elem: '#endtime'
        });

        form.render();

    });
}

Template.prototype.openadd = function (templateid) {
    top.mylayui.open({
        type: 2,
        title: '考评表编辑',
        shadeClose: true,
        shade: 0.5,
        area: ['850px', '500px'],
        content: 'workass/work_template_list_alone_edit?templateid=' + templateid
    });
}

Template.prototype.orgadd = function (templateid) {
    top.mylayui.open({
        type: 2,
        title: '应用部门',
        shadeClose: true,
        shade: 0.5,
        area: ['850px', '500px'],
        content: 'workass/work_templae_list_org_edit?templateid=' + templateid
    });
}

Template.prototype.addresult = function (result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        vWorkTemplate.searchtable();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}

Template.prototype.InitTable = function () {
    var table;
    var _this = this;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#list'
            , height: $(window).height() - 100
            , url: 'get_list' // 数据接口
            , page: true // 开启分页
            , limit: 30
            , id: 'templatelist'
            , method: "post"
            , cols: [[ // 表头
                {type: 'numbers'}
                , {field: 'templatename', title: '考评表名称', width: 150, align: 'center'}
                , {field: 'templatedes', title: '考评表说明', width: 200, align: 'center'}
                //, {field : 'begintime',title : '有效期',width : 200,align : 'center'}
                , {field: 'begintime', title: '考核期', width: 250, align: 'center'}
                , {field: 'byrealname', title: '创建人', width: 100, align: 'center'}
                , {title: '操作', width: 200, align: 'center', toolbar: '#operate'}
            ]]
            , request: {
                type: 'post'
            }
            , page: false
            , even: true //开启隔行背景
            //,size: 'sm' //小尺寸的表格
            , where: {templatename: $("#templatename").val()}
            , done: function (res, page, count) {
                var dataarr = res.data;


                $("[data-field='begintime']").children().each(function (index) {
                    var thisdata = dataarr[index - 1];
                    if (thisdata) {
                        var text = $(this).text();
                        var begin = (new Date(text)).format("yyyy-MM-dd");
                        var end = (new Date(thisdata.endtime)).format("yyyy-MM-dd");
                        $(this).text(begin + " 到  " + end);
                    }
                })
            }

        });


        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {

                top.mylayui.confirm('确定要删除吗？', {icon: 0}, function (index, dd) {
                    top.mylayui.close(index);
                    vWorkTemplate.deleteentity(data.templateid);
                });
            } else if (obj.event === 'edit') {
                vWorkTemplate.openadd(data.templateid);
            } else if (obj.event === 'org') {
                vWorkTemplate.orgadd(data.templateid);
            } else if (obj.event == 'submit') {
                top.mylayui.confirm('确定要提交吗？', {icon: 0}, function (index, dd) {
                    vWorkTemplate.submit(data.templateid);

                })
            }


            else {


                top.mylayui.open({
                    type: 2,
                    title: '考核打分',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['1400px', '768px'],
                    content: 'workass/work_kp_edit?read=0&templateid=' + data.templateid
                });
            }
        });


    });

    return table;
}

Template.prototype.deleteentity = function (templateid) {
    $.ajax({
        type: "POST",
        url: "delete_template",
        data: {templateid: templateid},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                document.location.reload();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}

Template.prototype.searchtable = function () {
    this.table.reload('templatelist', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , page: false
        , where: {
            templatename: $("#templatename").val()
        }
    });
}
Template.prototype.addresult = function (result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        document.location.reload();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}


Template.prototype.submit = function (templateid) {
    $.ajax({
        type: "POST",
        url: "submit",
        data: {templateid: templateid},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data=eval('('+data+')');
            if (data.code == 200) {
                top.mylayui.msg(data.msg, {icon: 1});

            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}
Template.prototype.addentityuserusass = function (templateid, orgids) {
    $.ajax({
        type: "POST",
        url: "create_templateorg",
        data: {templateid: templateid, origid: orgids},
        beforeSend: function (XHR) {
        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                top.mylayui.closeAll('iframe');
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}

var vWorkTemplate;
$(function () {
    vWorkTemplate = new Template();
})

function CloseDialog(result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        document.formedit.reset();
        document.location.reload();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}